<?php

namespace Api\Model;


class MedicalCommentModel extends BaseModel
{

    private static $instance;

    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    //获取评论总数
    public function getCommentCountByRecordId($record_id)
    {
        return $this->where(['record_id' => $record_id])->count();
    }

    //获取回复总数
    public function getReplyCountByRecordId($comment_id)
    {
        return $this->where(['parent_id' => $comment_id])->count();
    }

    //评论数+1
    public function incCommentCount($id)
    {
        $where['id'] = $id;
        $this->where($where)->setInc('comment_count');
    }

    //评论数-1
    public function decCommentCount($id)
    {
        $where['id'] = $id;
        $this->where($where)->setDec('comment_count');
    }


    //获取评论列表热门区域(一个置顶名额，一个点赞最高名额)
    public function getListByHotComment($record_id, $user_id)
    {
        $ids = [];
        $list = [];
        $like_record_id = 0;
        $top_record = $this->where(['record_id' => $record_id, 'parent_id' => 0, 'is_top' => 1, 'is_show' => 1])
            ->field(['id', 'user_id', 'comment', 'record_id', 'is_show_name', 'like_count', 'comment_count', 'create_time', 'is_top'])
            ->find();
        if ($top_record) {
            $ids[] = $top_record['id'];
        }
        $like_record = $this->where(['record_id' => $record_id, 'parent_id' => 0,'is_show'=>1])
            ->where('like_count', 'gt', 0)
            ->order('like_count desc')
            ->find();

        if ($like_record) {
            $ids[] = $like_record['id'];
            $like_record_id = $like_record['id'];
        }
        if ($ids) {
            $map['id'] = ['IN', $ids];
            $list = $this->where($map)->order('is_top desc')->select();
        }

        if ($list) {
            $list = $this->packData($list, $user_id, $record_id);
        }
        return [$ids, $list, $like_record_id];
    }

    //获取评论列表
    public function getListByComment($record_id, $pageno, $pageSize, $hot_ids, $user_id)
    {
        $where['record_id'] = $record_id;
        $where['parent_id'] = 0;
        $where['is_show'] = 1;
        if ($hot_ids) {
            $where['id'] = ['NOT IN', $hot_ids];
        }
        $rows = $this->where($where)
            ->field(['id', 'user_id', 'comment', 'record_id', 'is_show_name', 'like_count', 'comment_count', 'create_time', 'is_top'])
            ->order("create_time  DESC")
            ->limit(($pageno - 1) * $pageSize, $pageSize)
            ->select();
        $rows || $rows = [];

        if ($rows) {
            $rows = $this->packData($rows, $user_id, $record_id);
        }
        return $rows;
    }

    //获取回复列表
    public function getListByReply($comment_id, $pageno, $pageSize, $floor_user_id, $record_id, $user_id)
    {
        $where['parent_id'] = $comment_id;
        $where['is_show'] = 1;
        $rows = $this->where($where)
            ->field(['id', 'user_id', 'comment', 'record_id', 'is_show_name', 'create_time', 'parent_id'])
            ->order("create_time  DESC")
            ->limit(($pageno - 1) * $pageSize, $pageSize)
            ->select();
        $rows || $rows = [];
        if ($rows) {
            $user_ids = [];
            foreach ($rows as $k => $val) {
                if (!in_array($val['user_id'], $user_ids)) {
                    $user_ids[] = $val['user_id'];
                }
                $comment_ids[] = $val['id'];
            }
            $user_arr = UserModel::singleton()->getUserArrByIds($user_ids);
            //作者id
            $author_id = 0;
            $record = MedicalRecordModel::singleton()->find($record_id);
            $author_id = $record['user_id'];
            foreach ($rows as $key => $value) {
                $rows[$key]['name'] = $user_arr[$value['user_id']]['name'];
                $rows[$key]['avatarurl'] = $user_arr[$value['user_id']]['avatarurl'];
                $rows[$key]['create_time'] = date('Y/m/d H:i:s', strtotime($value['create_time']));
                $rows[$key]['is_author'] = $author_id == $value['user_id'] ? 1 : 0;
                $rows[$key]['is_floor'] = $floor_user_id == $value['user_id'] ? 1 : 0;
                $rows[$key]['can_delete'] = $user_id == $value['user_id'] ? 1 : 0;
            }
        }

        return $rows;
    }

    //评论详情
    public function getDetailById($comment_id)
    {
        $row = $this->where(['id' => $comment_id])
            ->field(['id', 'user_id', 'comment', 'record_id', 'is_show_name', 'like_count', 'comment_count', 'create_time', 'is_top'])
            ->find();
        $row['create_time'] = date('Y/m/d H:i:s', strtotime($row['create_time']));
        $user_arr = UserModel::singleton()->getUserArrByIds([$row['user_id']]);
        $row['name'] = $user_arr[$row['user_id']]['name'];
        $row['avatarurl'] = $user_arr[$row['user_id']]['avatarurl'];
        return $row;

    }


    //拼接列表数据
    private function packData($list, $user_id, $record_id)
    {
        $user_ids = [];
        $comment_ids = [];
        foreach ($list as $key1 => $val1) {
            if (!in_array($val1['user_id'], $user_ids)) {
                $user_ids[] = $val1['user_id'];
            }
            $comment_ids[] = $val1['id'];
        }
        $user_arr = UserModel::singleton()->getUserArrByIds($user_ids);
        $like_arr = MedicalCommentLikeModel::singleton()->getLikeArrByUserId($user_id, $comment_ids);
        //作者id
        $author_id = 0;
        $record = MedicalRecordModel::singleton()->find($record_id);
        $author_id = $record['user_id'];
        foreach ($list as $key2 => $val2) {
            $list[$key2]['name'] = $user_arr[$val2['user_id']]['name'];
            $list[$key2]['avatarurl'] = $user_arr[$val2['user_id']]['avatarurl'];
            $list[$key2]['create_time'] = date('Y/m/d H:i:s', strtotime($val2['create_time']));
            $list[$key2]['is_like'] = in_array($val2['id'], $like_arr) ? 1 : 0;
            $list[$key2]['is_author'] = $author_id == $val2['user_id'] ? 1 : 0;
            $list[$key2]['can_delete'] = $user_id == $val2['user_id'] ? 1 : 0;
        }
        return $list;
    }

    //置顶评论
    public function setTopComment($comment_id, $record_id)
    {
        //之前置顶的为0;
        $this->where(['record_id' => $record_id, 'is_top' => 1])->save(['is_top' => 0]);
        //现在的置顶
        $this->where(['id' => $comment_id])->save(['is_top' => 1]);
    }

    //取消评论
    public function setBottomComment($comment_id, $record_id)
    {
        //之前置顶的为0;
        //$this->where(['record_id'=>$record_id,'is_top'=>1])->save(['is_top'=>0]);
        //现在的置顶
        $this->where(['id' => $comment_id])->save(['is_top' => 0]);
    }

    //点赞数+1
    public function incLikeCount($id)
    {
        $where['id'] = $id;
        $this->where($where)->setInc('like_count');
    }

    //点赞数-1
    public function decLiketCount($id)
    {
        $where['id'] = $id;
        $this->where($where)->setDec('like_count');
    }

    //取消评论
    public function deleteComment($comment_id)
    {
        //现在的置顶
        $this->where(['id' => $comment_id])->save(['is_show' => 0]);
    }
}
